<?php

function member_favourite_restaurant_delete($rid){
    db_delete('ep_favourite_restaurant')
        ->condition('rid',$rid)
        ->execute();
    drupal_set_message("Sikeresen törölve a kedvencekből!");
    drupal_goto('member/menuk');
}

 
function member_getuser_favouriterestaurant_ids(){
    global $user;
    $query = db_select('ep_favourite_restaurant', 'fr')
            ->condition('uid',$user->uid,'=')
            ->fields('fr')
            ->execute();
    $rids=array();
    foreach($query as $favourite){
        $rids[]=$favourite->rid;
    }

    return $rids;  
}

function member_add_restaurant_to_favourite($rid){
    if (!in_array ($rid,member_getuser_favouriterestaurant_ids())){
     global $user;
     db_insert('ep_favourite_restaurant')
        ->fields(array(
            'uid'=>$user->uid,
            'rid'=>$rid
        ))
        ->execute();
     $message="Kedvencekhez adva!";
    }
    else{
        $message="Már a kedvencek között van!";
    }
    drupal_set_message($message);
    drupal_goto('member/menuk');
 }

function member_getuser_favouriteoffer_ids(){
    global $user;
    $query = db_select('ep_favourite_offer', 'fo')
            ->condition('uid',$user->uid,'=')
            ->fields('fo')
            ->execute();
    $oids=array();
    foreach($query as $favourite){
        $oids[]=$favourite->oid;
    }

    return $oids;  
}

function member_add_offer_to_favourite($oid){
    
    if (!in_array ($oid,member_getuser_favouriteoffer_ids())){
     global $user;
            db_insert('ep_favourite_offer')
                ->fields(array(
                'uid'=>$user->uid,
                'oid'=>$oid
            ))
            ->execute();
    $message="Kedvencekhez adva!";
    }
    else{
        $message="Már a kedvencek között van!";
    }
    drupal_set_message($message);
    drupal_goto('member/menuk');

}

function member_favourite_offer_delete($oid){
    db_delete('ep_favourite_offer')
        ->condition('oid',$oid)
        ->execute();
    drupal_set_message("Sikeresen törölve a kedvencekből!");
    drupal_goto('member/menuk');
}

function member_favourite_restaurant_page(){
    
}

function is_in_favourites($oid){
    if (in_array ($oid,member_getuser_favouriteoffer_ids())){
        return true;
    }
    else
        return false;
}

function member_add_offer_to_favourite_control($oid){
    global $base_url;
    if(is_in_favourites($oid)){
    return '<a href="'.url('/member/favourite_offer_delete/'.$oid).'">  <img src="'.url($base_url.'/'.drupal_get_path('module', 'member').'/javascript/remove.png').'"/></a>';
    }else
    return '<a href="'.url('/member/add_offer/'.$oid).'">  <img src="'.url($base_url.'/'.drupal_get_path('module', 'member').'/javascript/add.png').'"/></a>';
}

function is_in_favourites_restaurant($rid){
    if (in_array ($rid,member_getuser_favouriterestaurant_ids())){
        return true;
    }
    else
        return false;
}


function member_add_restaurant_to_favourite_control($rid){
    global $base_url;
    if(is_in_favourites_restaurant($rid)){
    return '<a href="'.url('/member/favourite_restaurant_delete/'.$rid).'">  <img src="'.url($base_url.'/'.drupal_get_path('module', 'member').'/javascript/remove.png').'"/></a>';
    }else
    return '<a href="'.url('/member/add_restaurant/'.$rid).'">  <img src="'.url($base_url.'/'.drupal_get_path('module', 'member').'/javascript/add.png').'"/></a>';
}




function member_get_offer_form($form,$form_state){
   $form['offer_search'] = array(
        '#type' => 'textfield',
        '#title' => t('Keresés:'),
         '#size' => 30,
        '#maxlength' => 64,);
   
   $form['submit']=array(
       '#type'=>'submit',
       '#value'=>t('Keresés'),
   );
   
   return $form;
}

function member_get_offer_form_submit($form, &$form_values){
        $query = db_select('ep_offers', 'o');
        $query->join('ep_restaurants', 'r', 'fr.rid = r.rid');
        $query->fields('o',array('offer','startdate','oid','price'))
                ->fields('r',array('name','rid'));
        $query -> condition('o.offer',$form_values['values']['offer_search'],'=');
        
        $results = $query
			->execute();    
   
}